package com.spark.lola.controller;

import com.spark.lola.statistics.entity.StatisticsProduct;
import com.spark.lola.statistics.entity.StatisticsProduct;
import com.spark.lola.statistics.entity.StatisticsPurchase;
import com.spark.lola.support.core.param.ExcelParams;
import com.spark.lola.support.mvc.service.ExcelServiceImpl;
import com.spark.lola.support.wms.constant.StatisticsGroupType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.mapper.statistics.StatisticsProductMapper;
import com.spark.lola.service.statistics.IStatisticsProductService;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * <p>
 * 产品报表 前端控制器
 * </p>
 * @author dengyejun
 * @since 2020-05-12
 */
@RestController
@RequestMapping("/api/statistics/product")
public class ProductController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private StatisticsProductMapper currentMapper;
    @Autowired
    private IStatisticsProductService currentService;
    @Autowired
    private ExcelServiceImpl excelService;

    @RequiresPermissions(value = "m6210")
    @RequestMapping(value = "/day/findBy", consumes = "application/json")
    public ResponseBean findDayBy(@RequestBody QueryParam<StatisticsProduct> queryBean,
                                  @SessionAttribute ShiroUser shiroUser) {
        PageParam<StatisticsProduct> pageParam = queryBean.toPageParam();
        IPage<StatisticsProduct> page = currentMapper.page(pageParam.getPage(), StatisticsGroupType.DAY, pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }



    @RequiresPermissions(value = "m6220")
    @RequestMapping(value = "/month/findBy", consumes = "application/json")
    public ResponseBean findMonthBy(@RequestBody QueryParam<StatisticsProduct> queryBean,
                                    @SessionAttribute ShiroUser shiroUser) {
        PageParam<StatisticsProduct> pageParam = queryBean.toPageParam();
        IPage<StatisticsProduct> page = currentMapper.page(pageParam.getPage(),StatisticsGroupType.MONTH, pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @RequiresPermissions(value = "m6230")
    @RequestMapping(value = "/year/findBy", consumes = "application/json")
    public ResponseBean findYearBy(@RequestBody QueryParam<StatisticsProduct> queryBean,
                                   @SessionAttribute ShiroUser shiroUser) {
        PageParam<StatisticsProduct> pageParam = queryBean.toPageParam();
        IPage<StatisticsProduct> page = currentMapper.page(pageParam.getPage(),StatisticsGroupType.YEAR, pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @RequiresPermissions(value = "m6240")
    @RequestMapping(value = "/top/findBy", consumes = "application/json")
    public ResponseBean findTopBy(@RequestBody QueryParam<StatisticsProduct> queryBean,
                                  @SessionAttribute ShiroUser shiroUser) {
        PageParam<StatisticsProduct> pageParam = queryBean.toPageParam();
        IPage<StatisticsProduct> page = currentMapper.page(pageParam.getPage(),StatisticsGroupType.SLEF, pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @RequiresPermissions(value = "m6211")
    @RequestMapping(value = "/day/excel-out", consumes = "application/json")
    public ResponseBean excelDayOut(@RequestBody ExcelParams<StatisticsProduct,QueryParam<StatisticsProduct>> excelParams, @SessionAttribute ShiroUser shiroUser){
        return excelOut(excelParams,shiroUser,StatisticsGroupType.DAY);
    }

    @RequiresPermissions(value = "m6221")
    @RequestMapping(value = "/month/excel-out", consumes = "application/json")
    public ResponseBean excelMonthOut(@RequestBody ExcelParams<StatisticsProduct,QueryParam<StatisticsProduct>> excelParams, @SessionAttribute ShiroUser shiroUser){
        return excelOut(excelParams,shiroUser,StatisticsGroupType.MONTH);
    }

    @RequiresPermissions(value = "m6231")
    @RequestMapping(value = "/year/excel-out", consumes = "application/json")
    public ResponseBean excelYearOut(@RequestBody ExcelParams<StatisticsProduct,QueryParam<StatisticsProduct>> excelParams, @SessionAttribute ShiroUser shiroUser){
        return excelOut(excelParams,shiroUser,StatisticsGroupType.YEAR);
    }

    @RequiresPermissions(value = "m6241")
    @RequestMapping(value = "/top/excel-out", consumes = "application/json")
    public ResponseBean excelTopOut(@RequestBody ExcelParams<StatisticsProduct,QueryParam<StatisticsProduct>> excelParams, @SessionAttribute ShiroUser shiroUser) {
        return excelOut(excelParams, shiroUser, StatisticsGroupType.SLEF);
    }

    private ResponseBean excelOut(ExcelParams<StatisticsProduct,QueryParam<StatisticsProduct>> excelParams, ShiroUser shiroUser, int groupType) {
        logger.info("===管理员【{}】申请生成货品报表【{}】Excel===START===", shiroUser.getLoginNo(), groupType);
        // 组装&调整查询条件
        PageParam<StatisticsProduct> pageParam = super.bindExcel(excelParams, null);
        IPage<StatisticsProduct> page = currentMapper.page(pageParam.getPage(), groupType, pageParam.getEntityWrapper());
        logger.info("===数据量【{}】待生成", page.getTotal());
        ResponseBean responseBean = excelService.create(excelParams.getFileds(), page.getRecords(), StatisticsProduct.class);
        logger.info("===管理员【{}】申请生成货品报表【{}】Excel===END===", shiroUser.getLoginNo(), groupType);
        return responseBean;
    }

}